package t327.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import t327.pojo.Expend;
import t327.pojo.font.LuiTableEntity;
import t327.pojo.until.Result;
import t327.service.iter.IExpendAndIncomeService;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("financeManager")
public class FinanceController {
    @Autowired
    private IExpendAndIncomeService expendAndIncomeService;

    @RequestMapping("getAllExpendAndIncome")
    public LuiTableEntity getAllExpendAndIncome(Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        LuiTableEntity luiTableEntity = new LuiTableEntity<Expend>();
        PageInfo<Expend> expendAndIncome = new PageInfo<>(expendAndIncomeService.getAllExpendAndIncome());
        List<Expend> expendAndIncomeList = expendAndIncome.getList();
        long total = expendAndIncome.getTotal();
        if(expendAndIncomeList!=null&&expendAndIncomeList.size()>0){
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(expendAndIncomeList);
        }else{
            luiTableEntity.setCode("203");
            luiTableEntity.setMsg("暂无数据");
        }
        return   luiTableEntity;
    }
    @RequestMapping("getExpendAndIncomeByCond")
    public LuiTableEntity getExpendAndIncomeByCond(Integer page, Integer limit, Long expendStatus){
        PageHelper.startPage(page,limit);
        LuiTableEntity luiTableEntity = new LuiTableEntity<Expend>();
        PageInfo<Expend> expendAndIncome = new PageInfo<>(expendAndIncomeService.getExpendAndIncomeByCond(expendStatus));
        List<Expend> expendAndIncomeList = expendAndIncome.getList();
        long total = expendAndIncome.getTotal();
        if(expendAndIncomeList!=null&&expendAndIncomeList.size()>0){
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(expendAndIncomeList);
        }else{
            luiTableEntity.setCode("203");
            luiTableEntity.setMsg("暂无数据");
        }
        return   luiTableEntity;
    }
    //添加支出
    @RequestMapping("addExpend")
    public Result addExpend(@RequestBody Expend expend){
        expend.setExpendStatus(1l);
        HashMap<String, String> map = expendAndIncomeService.addExpendAndIncome(expend);
        String code = map.get("code");
        if (code.equals("200")){
            return new Result(200,"添加成功",true,null);
        }
        return new Result(203,"添加失败",false,null);
    }
    //添加收入
    @RequestMapping("addIncome")
    public Result addIncome(@RequestBody Expend expend){
        expend.setExpendStatus(2l);
        HashMap<String, String> map = expendAndIncomeService.addExpendAndIncome(expend);
        String code = map.get("code");
        if (code.equals("200")){
            return new Result(200,"添加成功",true,null);
        }
        return new Result(203,"添加失败",false,null);
    }
    @RequestMapping("toOrderEntry")
    public String toOrderEntry(){
        return "finance/income";
    }
}
